/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld.demo;

import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;

/**
 *
 * @author thang
 * write sensor data in owl file
 * overwrite file: sensor_ontology.owl
 */
public class OntologyDB {

    //window data, 1 window has 5 set of data
    //each data set has 6 type: time, light, tempt, acceX, acceY, tiltZ
    private long[][] windowData = new long[5][6];

    public OntologyDB(int initValue){
        //init the window data to 0
        for (int i = 0; i < (windowData.length); i++){
            for (int j = 0; j < windowData[i].length; j++){
                windowData[i][j] = initValue;
            }
        }
    }

    // AcceX, AcceY sensor value in range about -1 to 1
    // convert to range 0 to 100
    public int convertScale(double in){
        double c1 = (int)(in * 100);

        //cut too high value
        if (c1 > 50){c1 = 50;}
        if (c1 < -50){c1 = -50;}

        //convet to positive integer;
        int r = (int)c1 + 50;

        return r;
    }

    //add a sample to window data
    public void addToWindow(long time, int light, int tempt, int acceX, int acceY, int tiltZ){
        //move 4 rows of data in the pass
        for (int i = windowData.length - 1; i > 0; i--){
            for (int j = 0; j < windowData[i].length; j++){
                windowData[i][j] = windowData[i - 1][j];
            }
        }
        
        //add current, new, data to windowData[0]
        windowData[0][0] = time;
        windowData[0][1] = light;
        windowData[0][2] = tempt;
        windowData[0][3] = acceX;
        windowData[0][4] = acceY;
        windowData[0][5] = tiltZ;
    }

    //just use: time, light, tempt, acceX, acceY, tiltZ
    public void addData(long time, int light, int tempt, double acceX, double acceY, double acceZ, double tiltX, double tiltY, double tiltZ){
        addToWindow(time, light, tempt, convertScale(acceX), convertScale(acceY), convertScale(tiltZ));
        writeToOWLFile();
    }

    //just writh acceX, acceY, tiltZ for driving at the moment
    public void writeToOWLFile(){
        PrintWriter outputStream = null;
        try{
            outputStream = new PrintWriter(new FileOutputStream("../../../ontology/driving.owl"));
        }
        catch(FileNotFoundException e){
            System.out.println("Error opening the file sensor_ontology.owl");
            System.exit(0);
        }
        System.out.println("Writing to file");
        outputStream.println("<?xml version=\"1.0\"?>");
        outputStream.println("<!DOCTYPE rdf:RDF [");
        outputStream.println("<!ENTITY soupa  \"http://pervasive.semanticweb.org/ont/2004/06/\">");
        outputStream.println("<!ENTITY rdf    \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">");
        outputStream.println("<!ENTITY rdfs   \"http://www.w3.org/2000/01/rdf-schema#\">");
        outputStream.println("<!ENTITY xsd    \"http://www.w3.org/2001/XMLSchema#\">");
        outputStream.println("<!ENTITY owl    \"http://www.w3.org/2002/07/owl#\">");
        outputStream.println();
        outputStream.println("   <!ENTITY foaf   \"http://xmlns.com/foaf/0.1#\">");
        outputStream.println("   <!ENTITY sen    \"http://www.latrobe.edu.au/mtdo/sunspot/sensor_ontology#\">");
        outputStream.println("   ]>");
        outputStream.println("<rdf:RDF");
        outputStream.println("   xmlns:rdf  = \"&rdf;\"");
        outputStream.println("   xmlns:rdfs = \"&rdfs;\"");
        outputStream.println("   xmlns:owl  = \"&owl;\"");
        outputStream.println("   xmlns:xsd  = \"&xsd;\"");
        outputStream.println("   xmlns:foaf = \"&foaf;\"");
        outputStream.println("   xmlns      = \"&sen;\"");
        outputStream.println("   xml:base   = \"http://www.latrobe.edu.au/mtdo/sunspot/sensor_ontology#\">");
        outputStream.println();
        outputStream.println("  <owl:Ontology rdf:about=\"&sen;sensor\">");
        outputStream.println("    <owl:versionInfo>cvs:$Revision: 1.0 $, $Date: 2010/05/27 11:53:17 $</owl:versionInfo>");
        outputStream.println("    <rdfs:label>Sensor Ontology</rdfs:label>");
        outputStream.println("    <rdfs:comment></rdfs:comment>");
        outputStream.println("  </owl:Ontology>");
        outputStream.println();
        outputStream.println("<owl:Class rdf:ID=\"Accelerometer\" />");
        outputStream.println();
        outputStream.println("<owl:DatatypeProperty rdf:ID=\"acceValue\">");
        outputStream.println("  <rdfs:domain rdf:resource=\"#Accelerometer\" />");
        outputStream.println("  <rdfs:range rdf:resource=\"&xsd;positiveInteger\" />");
        outputStream.println("</owl:DatatypeProperty>");
        outputStream.println();
        outputStream.println("<owl:Class rdf:ID=\"AcceX\">");
        outputStream.println("  <rdfs:subClassOf rdf:resource=\"#Accelerometer\" />");
        outputStream.println("</owl:Class>");
        outputStream.println();
        outputStream.println("<owl:Class rdf:ID=\"AcceY\">");
        outputStream.println("  <rdfs:subClassOf rdf:resource=\"#Accelerometer\" />");
        outputStream.println("</owl:Class>");
        outputStream.println();
        outputStream.println("<owl:Class rdf:ID=\"Tilt\" />");

        outputStream.println("<owl:DatatypeProperty rdf:ID=\"tiltValue\">");
        outputStream.println("  <rdfs:domain rdf:resource=\"#Tilt\" />");
        outputStream.println("  <rdfs:range rdf:resource=\"&xsd;positiveInteger\" />");
        outputStream.println("</owl:DatatypeProperty>");
        outputStream.println();
        outputStream.println("<owl:Class rdf:ID=\"TiltZ\">");
        outputStream.println("  <rdfs:subClassOf rdf:resource=\"#Tilt\" />");
        outputStream.println("</owl:Class>");
        outputStream.println();
        outputStream.println("<!-- first data set in window = latest, current data -->");
        outputStream.println("<!-- raw data range acce: -0.5 -> + 0.5, precision: 0.00 -->");
        outputStream.println("<!-- raw data range acce: -0.5 -> + 0.5, precision: 0.00 -->");
        outputStream.println("<!-- dlv data range: unsigned number -->");
        outputStream.println("<!-- convert: (raw data * 100) + 50 -> new range: 0 -> 100 -->");
        outputStream.println("<!-- with new range 50 meant o in raw data -->");
        outputStream.println("<!-- window data 1 -->");
        outputStream.println("<AcceX rdf:ID=\"AcceX1\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[0][3] + "</acceValue>"); // window data here
        outputStream.println("</AcceX>");
        outputStream.println("<AcceY rdf:ID=\"AcceY1\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[0][4] + "</acceValue>");
        outputStream.println("</AcceY>");
        outputStream.println("<TiltZ rdf:ID=\"TiltZ1\">");
        outputStream.println("  <tiltValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[0][5] + "</tiltValue>");
        outputStream.println("</TiltZ>");
        outputStream.println("<!-- window data 2 -->");
        outputStream.println("<AcceX rdf:ID=\"AcceX2\">");
        outputStream.println(  "<acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[1][3] + "</acceValue>");
        outputStream.println("</AcceX>");
        outputStream.println("<AcceY rdf:ID=\"AcceY2\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[1][4] + "</acceValue>");
        outputStream.println("</AcceY>");
        outputStream.println("<TiltZ rdf:ID=\"TiltZ2\">");
        outputStream.println("  <tiltValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[1][5] + "</tiltValue>");
        outputStream.println("</TiltZ>");
        outputStream.println("<!-- window data 3 -->");
        outputStream.println("<AcceX rdf:ID=\"AcceX3\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[2][3] + "</acceValue>");
        outputStream.println("</AcceX>");
        outputStream.println("<AcceY rdf:ID=\"AcceY3\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[2][4] + "</acceValue>");
        outputStream.println("</AcceY>");
        outputStream.println("<TiltZ rdf:ID=\"TiltZ3\">");
        outputStream.println("  <tiltValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[2][5] + "</tiltValue>");
        outputStream.println("</TiltZ>");
        outputStream.println("<!-- window data 4 -->");
        outputStream.println("<AcceX rdf:ID=\"AcceX4\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[3][3] + "</acceValue>");
        outputStream.println("</AcceX>");
        outputStream.println("<AcceY rdf:ID=\"AcceY4\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[3][3] + "</acceValue>");
        outputStream.println("</AcceY>");
        outputStream.println("<TiltZ rdf:ID=\"TiltZ4\">");
        outputStream.println("  <tiltValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[3][3] + "</tiltValue>");
        outputStream.println("</TiltZ>");
        outputStream.println("<!-- window data 5 -->");
        outputStream.println("<AcceX rdf:ID=\"AcceX5\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[4][3] + "</acceValue>");
        outputStream.println("</AcceX>");
        outputStream.println("<AcceY rdf:ID=\"AcceY5\">");
        outputStream.println("  <acceValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[4][4] + "</acceValue>");
        outputStream.println("</AcceY>");
        outputStream.println("<TiltZ rdf:ID=\"TiltZ5\">");
        outputStream.println("  <tiltValue rdf:datatype=\"&xsd;positiveInteger\">" + windowData[4][5] + "</tiltValue>");
        outputStream.println("</TiltZ>");
        outputStream.println("</rdf:RDF>");
        outputStream.println();
        outputStream.close();
    }
}
